<?php
class Node {

    private $LI = null;
    private $LIUL = null;
    private $treeData = null;
    private $ID = null;
    private $obj = null;

    public function input( $obj ) {
        $this->obj = $obj;
        return $this;
    }

    public function dataTree( $items, $id = 'id', $pid = 'sid', $child = 'child' ) {
        $tree = array(); //格式化好的树
        foreach ( $items as $item )
            if ( isset( $items[ $item[ $pid ] ] ) )
                $items[ $item[ $pid ] ][ $child ][ ] = &$items[ $item[ $id ] ];
            else
                $tree[ $item[ $id ] ] = &$items[ $item[ $id ] ];
        return $tree;
    }

    # 多级分类 数组列表
    public function treeArray( $result, $id = 0, $sid = "sid", $name = 'title', $icon = "├", $node = 0 ) {
        foreach ( $result as $v ) {
            if ( intval( $v[ $sid ] ) === intval( $id ) ) {
                $l = str_repeat( "&nbsp;&nbsp;", $node ) . ( $node ? $icon : "" );
                $v[ 'node' ] = $node;
                $v[ 'treeTitle' ] = $l . $v[ $name ];
                $this->treeData[ $v[ 'id' ] ] = $v;
                $this->treeArray( $result, $v[ 'id' ], $sid, $name, $icon, $node + 1 );
            }
        }
        return $this->treeData;
    }

    # 多级分类 li ul嵌套列表
    public function treeLIUL( $result, $id = 0, $sid = "sid", $name = 'title', $node = 0 ) {
        foreach ( $result as $v ) {
            if ( intval( $v[ $sid ] ) === intval( $id ) ) {
                if ( $node > 0 ) {
                    $this->LIUL .= "\n<ul class=\"child-{$v[$sid]} node-{$node}\">\n\t";
                }
                $c = $v[ 'model' ] == 'page' ? $v[ 'model' ] : 'sort';
                $url = $this->obj->url( $c, null, array( 'id' => $v[ 'id' ] ) );
                $this->LIUL .= "<li id=\"sort-{$v[ 'id' ]}\"><a href=\"{$url}\" title=\"{$v[$name]}\"><span>{$v[$name]}</span></a>";
                $this->treeLIUL( $result, $v[ 'id' ], $sid, $name, $node + 1 );
                $this->LIUL .= "</li>\n";
                if ( $node > 0 ) {
                    $this->LIUL .= "</ul>";
                }
            }
        }
        return $this->LIUL;
    }

    # 多级分类 li列表
    public function treeLI( $result, $id = 0, $sid = "sid", $name = 'title', $node = 0 ) {
        foreach ( $result as $v ) {
            if ( intval( $v[ $sid ] ) === intval( $id ) ) {
                $url = $this->obj->url( "sort", null, $v[ 'alias' ] ? array( 'alias' => $v[ 'alias' ] ) : array( 'id' => $v[ 'id' ] ) );
                $this->LI .= "
                    <li class=\"node{$node}\"><a href=\"{$url}\" title=\"{$v[$name]}\">{$v[$name]}</a></li>";
                $this->treeLI( $result, $v[ 'id' ], $sid, $name, $node + 1 );
            }
        }
        return $this->LI . "\n";
    }

    # 分类下所有子分类ID
    public function treeID( $arr, $id, $node = 0 ) {
        if ( $node <= 0 ) {
            $this->ID[ $id ] = $id;
        }
        foreach ( $arr as $v ) {
            if ( intval( $v[ 'sid' ] ) === intval( $id ) ) {
                $this->ID[ $v[ 'id' ] ] = $v[ 'id' ];
                $this->treeID( $arr, $v[ 'id' ], $node++ );
            }
        }
        return $this->ID;
    }

    # 数组排序
    public function arraySortByKey( $array, $key, $asc = true ) {
        $result = array();
        // 整理出准备排序的数组
        foreach ( $array as $k => $v ) {
            $values[ $k ] = isset( $v[ $key ] ) ? $v[ $key ] : '';
        }
        unset( $v );
        // 对需要排序键值进行排序
        $asc ? asort( $values ) : arsort( $values );
        // 重新排列原有数组
        foreach ( $values as $k => $v ) {
            $result[ $k ] = $array[ $k ];
        }
        return $result;
    }

    public function __destruct() {
        unset( $this->LI );
        unset( $this->LIUL );
        unset( $this->treeData );
        unset( $this->ID );
        unset( $this->obj );
    }
}